﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;

namespace FasterWPF
{
    public static class GroupBoxExt
    {
        /// <summary>
        /// Applies a new ControlTemplate to Remove Unnecessary White Borders and to Set the Corner Radius
        /// </summary>
        /// <param name="expa"></param>
        /// <param name="cornerRadius"></param>
        public static void ApplyGroupBoxStrategyToRemoveExtraWhiteBorders(this GroupBox expa, double cornerRadius)
        {
           // expa.ApplyTemplate();
            GroupBox tgb = expa;

            string xaml = "<ControlTemplate TargetType=\"{x:Type GroupBox}\">                <ControlTemplate.Resources><BorderGapMaskConverter x:Key=\"BorderGapMaskConverter\"/></ControlTemplate.Resources>          <Grid SnapsToDevicePixels=\"true\"><Grid.ColumnDefinitions><ColumnDefinition Width=\"6\" /><ColumnDefinition Width=\"Auto\" /><ColumnDefinition Width=\"*\" /><ColumnDefinition Width=\"6\" /></Grid.ColumnDefinitions><Grid.RowDefinitions>       <RowDefinition Height=\"Auto\" /><RowDefinition Height=\"Auto\" /><RowDefinition Height=\"*\" /><RowDefinition Height=\"6\" /></Grid.RowDefinitions><Border CornerRadius=\"" + (cornerRadius + 1) + "\" Grid.Row=\"1\" Grid.RowSpan=\"3\" Grid.Column=\"0\" Grid.ColumnSpan=\"4\" BorderThickness=\"{TemplateBinding BorderThickness}\" BorderBrush=\"Transparent\" Background=\"{TemplateBinding Background}\" /><Border Name=\"Header\" Padding=\"3,1,3,0\" Grid.Row=\"0\" Grid.RowSpan=\"2\" Grid.Column=\"1\"><ContentPresenter ContentSource=\"Header\" RecognizesAccessKey=\"true\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\" /></Border><ContentPresenter Grid.Row=\"2\" Grid.Column=\"1\" Grid.ColumnSpan=\"2\" Margin=\"{TemplateBinding Padding}\" SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\" /><Border Grid.Row=\"1\" Grid.RowSpan=\"3\" Grid.ColumnSpan=\"4\"  BorderThickness=\"{TemplateBinding BorderThickness}\" BorderBrush=\"{TemplateBinding BorderBrush}\" CornerRadius=\"" + cornerRadius + "\"><Border.OpacityMask><MultiBinding Converter=\"{StaticResource BorderGapMaskConverter}\" ConverterParameter=\"7\"><Binding ElementName=\"Header\" Path=\"ActualWidth\" /><Binding Path=\"ActualWidth\" RelativeSource=\"{RelativeSource Self}\" /><Binding Path=\"ActualHeight\" RelativeSource=\"{RelativeSource Self}\" /></MultiBinding></Border.OpacityMask></Border></Grid></ControlTemplate>";

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
            ParserContext pc = new ParserContext();
            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            var repBtnTemplate = (ControlTemplate)XamlReader.Load(sr, pc);
            tgb.Template = repBtnTemplate;
        }

        public static GroupBox SetBackgroundColor(this GroupBox chain, Brush brush)
        {
            Brush newBrush = (brush);
            chain.Background = newBrush;
            return chain;
        }

        public static GroupBox Initialize(this GroupBox source, double width, double height, ContainerType containerType, int? rowSpan, int? colSpan)
        {
            if (rowSpan != null)
            {
                Grid.SetRowSpan(source, (int)rowSpan);
            }

            if (colSpan != null)
            {
                Grid.SetColumnSpan(source, (int)colSpan);
            }

            return Initialize(source, width, height, containerType); //Default width and height to Auto by setting to NaN
        }

        public static GroupBox Initialize(this GroupBox source, double width, double height, ContainerType containerType)
        {   
            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.Width = width;
            source.Height = height;
            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<GroupBox>(source);
            source.SetIComposite<GroupBox>(icomp);

            GroupBoxParent gbsr = new GroupBoxParent(); //IParent
            source.StoreSetting<GroupBox>("IParent", gbsr);

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<GroupBox>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) });

            return source;
        }
        
    }//end of class
}//end of namespace
